/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package DAO;

import ObjetData.Categorie;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.HashMap;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author Administrateur
 */
public class DaoCategorie extends DAO<Categorie> {

    private HashMap<Integer, Categorie> categories = new HashMap<Integer, Categorie>();

    @Override
    public boolean delete(Categorie obj) {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    @Override
    public boolean save(Categorie obj) {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    @Override
    public Categorie find(int id) {
        if (id > 0) {
            Categorie cat1 = new Categorie();
            ResultSet result = null;
            try {
                result = Connect.getConnexion().createStatement(
                        ResultSet.TYPE_SCROLL_INSENSITIVE,
                        ResultSet.CONCUR_READ_ONLY).executeQuery(
                        "SELECT ID_CATEGORIE, LABEL FROM APP.CATEGORIES "
                        + " where ID_CATEGORIE = " + id);
                if (result.first()) {
                    result.first();
                    cat1.setIdCategorie(result.getInt("ID_CATEGORIE"));
                    cat1.setLabelCat(result.getString("LABEL"));
                }

                //return (Animal) result.first();
            } catch (SQLException ex) {
                Logger.getLogger(DaoAnimal.class.getName()).log(Level.SEVERE, null, ex);
            }
            return cat1;
        } else {
            throw new DaoException("Animal d'id [" + id + "] inconnue", 2);
        }
    }

    @Override
    public HashMap<Integer, Categorie> getAll() {
        try {
            ResultSet result = Connect.getConnexion().createStatement(
                    ResultSet.TYPE_SCROLL_INSENSITIVE,
                    ResultSet.CONCUR_READ_ONLY).executeQuery(
                    "SELECT ID_CATEGORIE, LABEL FROM APP.CATEGORIES order by LABEL");
            while (result.next()) {
                Categorie cat1 = new Categorie(result.getInt("ID_CATEGORIE"), result.getString("LABEL"));
                categories.put(result.getInt(1), cat1);
            }
        } catch (SQLException ex) {
            Logger.getLogger(DaoAnimal.class.getName()).log(Level.SEVERE, null, ex);
        }
        return this.categories;
    }

    @Override
    public HashMap<Integer, Categorie> search(String searchValue, Integer idCatValue) {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    public HashMap<Integer, HashMap<Integer, Integer>> getCountByCatEsp() {
        HashMap<Integer, HashMap<Integer, Integer>> list = new HashMap<Integer, HashMap<Integer, Integer>>();
        String li;
        try {
            ResultSet result = Connect.getConnexion().createStatement(
                    ResultSet.TYPE_SCROLL_INSENSITIVE,
                    ResultSet.CONCUR_READ_ONLY).executeQuery(
                    "SELECT ID_CATEGORIE, ID_ESPECE,  count(ID_ARTICLE) SOMME from APP.ARTICLE " //ou count(ID_ARTICLE) ou SUM(QUANTITE)
                    + " inner join APP.CATEGORIES  using(ID_CATEGORIE) "
                    + " inner join APP.ESPECE  using(ID_ESPECE) "
                    + " group by ID_CATEGORIE, ID_ESPECE");
            while (result.next()) {
                if (list.containsKey(result.getInt("ID_CATEGORIE"))) {
                    list.get(result.getInt("ID_CATEGORIE")).put(result.getInt("ID_ESPECE"), result.getInt("SOMME"));
                } else {
                    list.put(result.getInt("ID_CATEGORIE"), new HashMap<Integer, Integer>());
                    list.get(result.getInt("ID_CATEGORIE")).put(result.getInt("ID_ESPECE"), result.getInt("SOMME"));
                }
            }
        } catch (SQLException ex) {
            //Logger.getLogger(DaoAnimal.class.getName()).log(Level.SEVERE, null, ex);
            li = ex.getMessage();
        }
        //System.out.println(list);
        return list;
    }
}
